package com.x.erp.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.model.Rotation;
import com.x.erp.rpc.RotationRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 轮播图管理
 * @author gdf
 */
@RestController
public class RotationController extends BaseController {

    @Reference
    private RotationRpcService rotationRpcService;

	@Autowired
	private FastFileStorageClient fdfsClient;//文件服务
	
	/**
	 * 轮播图新增
	 * @param rotation
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/insertOrUpdateRotation", method = RequestMethod.POST)
	public ResultModel insertOrUpdateRotation(Rotation rotation, MultipartFile file) {

		if(!this.isNotNull(rotation.getRotationName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "轮播图名称不能为空！");
		}
		if(rotation.getRotationSeq() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "轮播图排序不能为空！");
		}
		if(rotation.getRotationShowSeconds() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "轮播图显示时间不能为空！");
		}
		String fdfsId = null;
		if(file != null && !file.isEmpty()){
			String delFileId = rotation.getRotationImage();
			//上传图片
			try {
				fdfsId = fdfsClient.uploadFile(file.getInputStream(), "png");
				if(fdfsId == null){
					return new ResultModel(ResultCode.RESULT_FAIL, "图片上传服务器失败，请重新提交数据！");
				}
				rotation.setRotationImage(fdfsId);
			} catch (Exception e) {
				e.printStackTrace();
				return new ResultModel(ResultCode.RESULT_FAIL, "图片上传服务器失败，请重新提交数据！");
			}
			//删除原有图片
			if(StringUtils.isNotEmpty(delFileId)){
				try {
					fdfsClient.deleteFile(delFileId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("fdfsId =======" +fdfsId);
		if(this.isNotNull(rotation.getRotationId())){
			rotationRpcService.updateRotation(rotation);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "修改轮播图成功！");
		}else{
			rotation.setRotationImage(fdfsId);
			rotationRpcService.insertRotation(rotation);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "新增轮播图成功！");
		}
	}
	
	/**
	 * 获取轮播图列表
	 * @param rotation
	 * @return
	 */
	@RequestMapping(value = "/getRotationList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectRotationList(Rotation rotation){
		List<Rotation> list = rotationRpcService.selectRotationList(rotation);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", list);
	}

	/**
	 * 获取轮播图数量
	 * @param rotation
	 * @return
	 */
	@RequestMapping(value = "/getRotationListNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getRotationListNum(Rotation rotation){
		Map<String, Object> res = new HashMap<>();
		res.put("total", rotationRpcService.selectRotationListNum(rotation));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 修改轮播图状态
	 * @param rotation
	 * @return
	 */
	@RequestMapping(value = "/updateRotationState", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateRotationState(Rotation rotation){
		if(!this.isNotNull(rotation.getRotationId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "轮播图ID不能为空！");
		}
		if(rotation.getRotationState() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "轮播图状态不能为空！");
		}
		rotationRpcService.updateRotation(rotation);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功");
	}
	
}
